const express = require('express');
const multer = require('multer');
const path = require('path');
const cors = require('cors');

const app = express();
const PORT = 3000; // Node.js 的端口，可以选择任何未被占用的端口

// 启用 CORS
app.use(cors());

// 设置 multer 存储配置
const storage = multer.diskStorage({
    destination: (req, file, cb) => {
        cb(null, 'D:/2307/imgs'); // 上传路径
    },
    filename: (req, file, cb) => {
        cb(null, Date.now() + path.extname(file.originalname)); // 文件重命名
    },
});

const upload = multer({ storage });

// 上传接口
app.post('/api/upload', upload.single('file'), (req, res) => {
    if (!req.file) {
        return res.status(400).send('No file uploaded.');
    }
    res.json({ message: 'File uploaded successfully', file: req.file });
});

app.listen(PORT, () => {
    console.log(`Server is running on http://localhost:${PORT}`);
});
